package com.example.textswitchtestdemo01.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.bean.Car;
import com.example.textswitchtestdemo01.bean.UserAppointment;
import com.example.textswitchtestdemo01.util.InitToolbar;
import com.example.textswitchtestdemo01.util.RetrofitUtil;
import com.google.android.material.tabs.TabLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FourthActivity extends AppCompatActivity {

    public static final String TAG = "FourthActivity>>>";
    private TabLayout tl_fourth;
    private ViewPager vp_fourth;
    private ArrayList<View> views = new ArrayList<>();
    private String[] titles = {"车辆定制", "订单管理"};
    private ArrayList<Car> cars = new ArrayList<>();
    private ArrayAdapter<UserAppointment.DataBean> arrayAdapter;
    private List<UserAppointment.DataBean> list = new ArrayList<>();
    private RadioButton rb_typeone;
    private RadioButton rb_typetwo;
    private RadioButton rb_typethree;
    private Button btn_time;
    private EditText et_count;
    private Spinner sp_pailiang;
    private RadioGroup rg_biansu;
    private RadioGroup rg_luntai;
    private RadioGroup rg_zhongkong;
    private RadioGroup rg_shache;
    private Spinner sp_xuangua;
    private Button btn_xiadingdan;
    private int chooesType = 0;
    private String chooesEngine="1";
    private String[] hangArr;
    private String chooesHang="1";
    private String chooesSpeedBox="1";
    private String chooesWheel="1";
    private String chooesCenterCrtl="1";
    private String chooesBrake="1";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth);
        InitToolbar.initToolbar(this, "第四页", true);
        hangArr = getResources().getStringArray(R.array.xuangua);
        initView();

        getData();
    }

    private void getData() {
        RetrofitUtil.getAllUserAppointment(new Callback<UserAppointment>() {
            @Override
            public void onResponse(Call<UserAppointment> call, Response<UserAppointment> response) {
                UserAppointment body = response.body();
                Toast.makeText(FourthActivity.this, body.getMessage(), Toast.LENGTH_SHORT).show();
                List<UserAppointment.DataBean> data = body.getData();
                if (!data.isEmpty()) {
                    list.clear();
                    list.addAll(data);
                }
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<UserAppointment> call, Throwable t) {
                Toast.makeText(FourthActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @SuppressLint("InflateParams")
    private void initView() {
        tl_fourth = (TabLayout) findViewById(R.id.tl_fourth);
        vp_fourth = (ViewPager) findViewById(R.id.vp_fourth);

        tl_fourth.setupWithViewPager(vp_fourth);

        LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
        views.add(layoutInflater.inflate(R.layout.item_vp_car, null));
        views.add(layoutInflater.inflate(R.layout.item_vp_manager, null));

        vp_fourth.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return views.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return (view == object);
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(views.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(views.get(position));
                return views.get(position);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });
        vp_fourth.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (0 == position) {
                    getData();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initCarView();
        initSecondView();

    }

    private void initSecondView() {
        rb_typeone = views.get(1).findViewById(R.id.rb_typeone);
        rb_typeone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_typeone.setChecked(true);
                rb_typetwo.setChecked(false);
                rb_typethree.setChecked(false);
                chooesType = 1;
            }
        });
        rb_typetwo = views.get(1).findViewById(R.id.rb_typetwo);
        rb_typetwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_typeone.setChecked(false);
                rb_typetwo.setChecked(true);
                rb_typethree.setChecked(false);
                chooesType = 2;
            }
        });
        rb_typethree = views.get(1).findViewById(R.id.rb_typethree);
        rb_typethree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_typeone.setChecked(false);
                rb_typetwo.setChecked(false);
                rb_typethree.setChecked(true);
                chooesType = 3;
            }
        });
        rb_typeone.setChecked(true);

        btn_time = views.get(1).findViewById(R.id.btn_time);
        btn_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2020/1/10
            }
        });
        et_count = views.get(1).findViewById(R.id.et_count);
        sp_pailiang = views.get(1).findViewById(R.id.sp_pailiang);
        sp_pailiang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                chooesEngine = position + 1 + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        rg_biansu = views.get(1).findViewById(R.id.rg_biansu);
        rg_biansu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_biansu_zidong) {
                    chooesSpeedBox = "0";
                } else {
                    chooesSpeedBox = "1";
                }

            }
        });

        rg_luntai = views.get(1).findViewById(R.id.rg_luntai);
        rg_luntai.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_luntai_kao) {
                    chooesWheel = "0";
                } else {
                    chooesWheel = "1";
                }
            }
        });

        rg_zhongkong = views.get(1).findViewById(R.id.rg_zhongkong);
        rg_zhongkong.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_zhongkong_low) {
                    chooesCenterCrtl = "0";
                } else {
                    chooesCenterCrtl = "1";
                }
            }
        });

        rg_shache = views.get(1).findViewById(R.id.rg_shache);
        rg_shache.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_shache_gu) {
                    chooesBrake = "0";
                } else {
                    chooesBrake = "1";
                }
            }
        });
        sp_xuangua = views.get(1).findViewById(R.id.sp_xuangua);
        sp_xuangua.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                chooesEngine = position + 1 + "";
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        btn_xiadingdan = views.get(1).findViewById(R.id.btn_xiadingdan);
        btn_xiadingdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int l = (int) (System.currentTimeMillis() / 1000L);
                RetrofitUtil.addUserAppointment(new UserAppointment.DataBean(1, "77", "...",
                        chooesType, 21, l, 1, 2000,
                        chooesEngine, chooesSpeedBox, chooesWheel, chooesCenterCrtl, chooesBrake, chooesHang, "1"), new Callback<UserAppointment>() {
                    @Override
                    public void onResponse(Call<UserAppointment> call, Response<UserAppointment> response) {
                        Log.d(TAG, "onResponse: " + response.body().getData().get(0).toString());
                        Toast.makeText(FourthActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<UserAppointment> call, Throwable t) {
                        Log.e(TAG, "onFailure: ", t);
                    }
                });
            }
        });
    }

    private void initCarView() {
        ListView carLV = views.get(0).findViewById(R.id.lv_car);
        arrayAdapter = new ArrayAdapter<UserAppointment.DataBean>(getApplicationContext(), R.layout.item_lv_car, list) {

            class ViewHolder {
                View rootView;
                TextView tv_type;
                TextView tv_time;
                TextView tv_count;
                TextView tv_pailiang;
                TextView tv_biansu;
                TextView tv_luntai;
                TextView tv_zhongkong;
                TextView tv_shache;
                TextView tv_xuangua;

                ViewHolder(View rootView) {
                    this.rootView = rootView;
                    this.tv_type = (TextView) rootView.findViewById(R.id.tv_type);
                    this.tv_time = (TextView) rootView.findViewById(R.id.tv_time);
                    this.tv_count = (TextView) rootView.findViewById(R.id.tv_count);
                    this.tv_pailiang = (TextView) rootView.findViewById(R.id.tv_pailiang);
                    this.tv_biansu = (TextView) rootView.findViewById(R.id.tv_biansu);
                    this.tv_luntai = (TextView) rootView.findViewById(R.id.tv_luntai);
                    this.tv_zhongkong = (TextView) rootView.findViewById(R.id.tv_zhongkong);
                    this.tv_shache = (TextView) rootView.findViewById(R.id.tv_shache);
                    this.tv_xuangua = (TextView) rootView.findViewById(R.id.tv_xuangua);
                }
            }

            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_lv_car, parent, false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }

                UserAppointment.DataBean item = getItem(position);

                String speed = item.getSpeed();
                if (speed == null) {
                    return convertView;
                }
                holder.tv_biansu.setText(speed.equals("0") ? "自动" : "手动");
                holder.tv_count.setText(item.getNum() + "");
                holder.tv_luntai.setText(item.getWheel().equals("0") ? "烤漆" : "电镀");
                holder.tv_pailiang.setText(getEngineLevel(item.getEngine()));
                holder.tv_shache.setText(item.getBrake().equals("0") ? "鼓式制动器" : "盘式制动器");
                String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getTime() * 1000L);
                holder.tv_time.setText(format);

                holder.tv_type.setText(getCarType(item.getType()));

                //holder.tv_xuangua.setText(getHangType(item.getHang()));
                holder.tv_xuangua.setText(hangArr[Integer.parseInt(item.getHang())]);
                holder.tv_zhongkong.setText(item.getControl().equals("0") ? "低配" : "高配");
                return convertView;
            }
        };
        carLV.setAdapter(arrayAdapter);

    }

    private String getCarType(int i) {
        switch (i) {
            case 1:
                return "轿车";
            case 2:
                return "MPV";
            case 3:
                return "SUV";
        }
        return "SUV";
    }

    private String getHangType(String hang) {
        switch (hang) {
            case "1":
                return "独立悬挂系统";
            case "2":
                return "主动悬挂系统";
            case "3":
                return "横臂式悬挂系统";
            case "4":
                return "纵臂式悬挂系统";
            case "5":
                return "烛式悬挂系统";
            case "6":
                return "多连杆式悬挂系统";
            case "7":
                return "麦弗逊式悬挂系统";
        }
        return "麦弗逊式悬挂系统";

    }


    private String getEngineLevel(String engine) {
        switch (engine) {
            case "0":
                return "1.0";
            case "1":
                return "1.5";
            case "2":
                return "2.0";
            case "3":
                return "2.5";
            case "4":
                return "3.0";
            case "5":
                return "4.0";
        }
        return "1.0";
    }


}
